package cn.gson.boot.model.service;

import cn.gson.boot.model.dao.GuestDao;
import cn.gson.boot.model.mapper.GuestMapper;
import cn.gson.boot.model.pojos.Guest;
import cn.gson.boot.model.pojos.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class GuestService {
    @Autowired
    GuestMapper guestMapper;

    @Autowired
    GuestDao guestDao;

    //分页查询和模糊查询
    public List<Guest> allGuests(String gLianxir,String gLxfshi,String gGongsi,
                                 String gXuqiu,String gBeizhu,String gState,String gRizhi,int empId){
        return guestMapper.allGuests(gLianxir,gLxfshi,gGongsi,gBeizhu,gXuqiu,gState,gRizhi,empId);
    }

    //通过id查询
    public Guest xqGuests(Integer gId){
        return guestMapper.xqGuests(gId);
    }

    //新增
    public void addGuest(Guest guest, HttpSession session){
        System.out.println("新增了");
        Date date = new  Date();
        Timestamp tt = new Timestamp(date.getTime());
        int id = guest.getgId();
        System.out.println("客户编号："+id);
        User us =  (User) session.getAttribute("user");
        System.err.println("userSession:"+us);
        if(id==0){
            User user = new User(us.getEmpId());
            guest.setgState("未处理");
            guest.setgStates(0);
            guest.setEmpId(user);
            guest.setgFl("录入");
            guest.setgTjshij(tt);
            guestDao.save(guest);
        }else{
            System.out.println("修改了");
            Guest g2 = guestDao.findById(id).get();
            g2.setgLaiy(guest.getgLaiy());//来源
            g2.setgCben(guest.getgCben());//成本
            g2.setgBeizhu(guest.getgBeizhu());//备注
            g2.setgGongsi(guest.getgGongsi());//公司
            g2.setgLianxir(guest.getgLianxir());//联系人
            g2.setgLxfshi(guest.getgLxfshi());//联系方式
            g2.setgXuqiu(guest.getgXuqiu());//需求
            System.out.println("对象"+g2);
            guestDao.save(g2);
        }
    }

    //修改获客表的无效
    public  void xiugaiGuest(Integer gId,String leixing){
        //获取页面id
        Guest guest = guestDao.findById(gId).get();
        //赋值
        System.out.println("类型"+leixing);
        if(leixing=="加为客户" || leixing.equals("加为客户")){
            System.out.println("进来加为客户了");
            guest.setgRizhi("加客户");//日志
            guest.setgState("已处理");//处理状态（文字）
            guest.setgStates(1);//处理状态（数字）
            guestDao.save(guest);//执行修改
        }else if(leixing=="设为无效" || leixing.equals("设为无效")){
            System.out.println("进来设为无效了");
            guest.setgRizhi("失效数据");//日志
            guest.setgState("已处理");//处理状态（文字）
            guest.setgStates(1);//处理状态（数字）
            guestDao.save(guest);//执行修改
        }
    }

    //删除
    public void deleteGuest(int id){
        guestDao.deleteById(id);
    }

    //
}
